package cn.lyjuan.android.demofive.ui.slide.section;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import cn.lyjuan.android.demofive.R;
import cn.lyjuan.android.mylibrary.view.LotterSortView;

/**
 * 动态生成数据会有性能问题，而且这里在的分组插件有性能问题
 * Created by chad on 03/11/2017.
 */
public class SectionActivity extends Activity implements LotterSortView.OnWordsChangeListener
{
    private ListView listView;
    private LotterSortView sortView;
    private SectionAdapter adp;
    private TextView indexText;
    private EditText inputText;

    private List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ui_slide_section_activity);

        loadView();

        setView();

        bindListener();

        loadDate();
    }

    private void loadView()
    {
        listView = (ListView) findViewById(R.id.section_list);
        sortView = (LotterSortView) findViewById(R.id.section_index);
        indexText = (TextView) findViewById(R.id.section_index_text);
        inputText = (EditText) findViewById(R.id.section_input);
    }

    private void setView()
    {
        adp = new SectionAdapter(this, list);
        listView.setAdapter(adp);
    }

    private void loadDate()
    {
        Random rdm = new Random();
        for (int i = 0; i < LotterSortView.words.length; i++)
        {
            int base = rdm.nextInt(10) + 1;
            for (int j = 0; j < base; j++)
            {
                String rdmStr = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
                list.add(LotterSortView.words[i] + rdmStr);
            }
        }

        adp.notifyDataSetChanged();
    }

    @Override
    public void wordsChange(int section, String content)
    {
        listView.setSelection(adp.getSectionPostionForSection(section));
        indexText.setText(content);
        indexText.setVisibility(View.VISIBLE);
        handler.removeMessages(1);
        handler.sendEmptyMessageDelayed(1, 500);
    }

    private void bindListener()
    {
        sortView.setOnWordsChangeListener(this);
        inputText.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                // 执行过滤
                adp.getFilter().filter(inputText.getText());
            }

            @Override
            public void afterTextChanged(Editable s)
            {
            }
        });
    }

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            indexText.setVisibility(View.INVISIBLE);
        }
    };
}
